using System;
using System.Xml;

namespace Icodeon.Util
{
    public enum ScormType
    {
        asset,
        sco
    }

    public enum ScormVersion
    {
        scorm1p2,
        scorm2004_2ed,
        scorm2004_3ed,
        scorm2004_4ed
    }

    public class ScormUtils
    {
        private XmlUtils xmlUtils;

        public ScormUtils()
        {
            this.xmlUtils = new XmlUtils();
        }

        public ScormVersion GetScormVersion(XmlElement manifest)
        {
            if (!manifest.LocalName.Equals("manifest"))
            {
                throw new ArgumentException("XmlElement must be a manifest: " + manifest.LocalName);
            }

            XmlElement metadata = this.xmlUtils.GetNamedChildElement(manifest, "metadata");
            if (null != metadata)
            {
                XmlElement schemaversion = this.xmlUtils.GetNamedChildElement(metadata, "schemaversion");
                if (null != schemaversion)
                {
                    switch (schemaversion.InnerText)
                    {
                        case "CAM 1.3":
                            return ScormVersion.scorm2004_2ed;
                        case "2004 3rd Edition":
                            return ScormVersion.scorm2004_3ed;
                        case "2004 4th Edition":
                            return ScormVersion.scorm2004_4ed;
                        default:
                            return ScormVersion.scorm1p2;
                    }
                }
            }
            return ScormVersion.scorm1p2;
        }

        public ScormType GetScormType(XmlElement resource, ScormVersion scormVersion)
        {
            if (!resource.LocalName.Equals("resource"))
            {
                throw new ArgumentException("XmlElement must be a resource: " + resource.LocalName);
            }

            string attributeName = "scormtype";
            if (scormVersion != ScormVersion.scorm1p2)
            {
                attributeName = "scormType";
            }

            string scormType = this.xmlUtils.GetAttributeValue(resource, attributeName);
            switch (scormType)
            {
                case "asset":
                    return ScormType.asset;
                case "sco":
                    return ScormType.sco;
                default:
                    return ScormType.asset;
            }
            
        }
    }
}
